package com.st.rzs.solr.connection;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

/**
 * 
 * @desc:Solr链接工厂
 * @author wangsh
 * @date 2019-01-09 下午8:15:36
 */
public class SolrConncetionFactory {

	// 连接池
	private static Map<String, HttpSolrServer> conncetions = new HashMap<String, HttpSolrServer>();
	// solr集群连接池
	private static Map<String, CloudSolrServer> cloudConncetions = new HashMap<String, CloudSolrServer>();

	/**
	 * @desc:获取连接
	 * @param connectionName
	 * @return
	 * @throws MartianException
	 */
	public static HttpSolrServer getConnection(String connectionName) throws Exception {
		if (!conncetions.containsKey(connectionName)) {
			synchronized (SolrConncetionFactory.class) {
				if (!conncetions.containsKey(connectionName)) {
					SolrConncetion conn = new SolrConncetion();
					HttpSolrServer connection = conn.getConnection(connectionName);
					conncetions.put(connectionName, connection);
				}
			}
		}
		return conncetions.get(connectionName);
	}

	/**
	 * @desc:获取连接
	 * @param connectionName
	 * @return
	 * @throws MalformedURLException
	 * @throws MartianException
	 */
	public static CloudSolrServer getColudConnection(String connectionName) throws Exception {
		if (!cloudConncetions.containsKey(connectionName)) {
			synchronized (SolrConncetionFactory.class) {
				if (!cloudConncetions.containsKey(connectionName)) {
					SolrConncetion conn = new SolrConncetion();
					CloudSolrServer connection = conn.getConnection2(connectionName);
					cloudConncetions.put(connectionName, connection);
				}
			}
		}
		return cloudConncetions.get(connectionName);
	}

	public static void main(String[] args) {

	}
}
